#include <QCoreApplication>
#include <QVector>
#include <QJsonObject>
#include "serverdata.h"
#include "jsonconfig.h"

void set(const ServerData &data)
{
    QString s1 = data.getHost();
    QString s2 = const_cast<ServerData &>(data).getHost();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    JsonConfig<ServerData> servCfg("servercfg.json");
    servCfg.load();

    /* 方法1 */
    QJsonObject obj;
    obj.insert("host", "127.0.0.1");
    obj.insert("port", 8801);

    QString host1 = obj["host"].toString();
    int     port1 = obj["port"].toInt();

    servCfg.add(obj);

    /* 方法2 */
    ServerData data2;
    data2.insert("host", "127.0.0.2");
    data2.insert("port", 8802);

    QString host2 = data2["host"].toString();
    int     port2 = data2["port"].toInt();
    servCfg.add(data2);

    /* 方法3，需要自己实现 */
    ServerData data3;
    data3.setHost("127.0.0.3");
    data3.setPort(8803);

    QString host3 = data3.getHost();
    int     port3 = data3.getPort();
    servCfg.add(data3);

    /* 方法4 */
    ServerData data4 = data3;
    data4["host"] = "127.0.0.4";
    data4.setPort(8004);
    servCfg.add(data4);


    QVector<ServerData> &vec = servCfg.configs();
    for (int i = 0; i < (int)vec.size(); ++i)
    {
        printf("1-- %s, %d\n", vec[i].getHost().toStdString().c_str(), vec[i].getPort());
        printf("2-- %s, %d\n", vec[i]["host"].toString().toStdString().c_str(), vec[i]["port"].toInt());
    }
    printf("\n");

    servCfg.del(data4);
    for (int i = 0; i < (int)vec.size(); ++i)
    {
        printf("1-- %s, %d\n", vec[i].getHost().toStdString().c_str(), vec[i].getPort());
        printf("2-- %s, %d\n", vec[i]["host"].toString().toStdString().c_str(), vec[i]["port"].toInt());
    }
    printf("\n");

    ServerData data5;
    data5["host"] = "127.0.0.5";
    data5["port"] = 8005;
    servCfg.mod(data3, data5);
    for (int i = 0; i < (int)vec.size(); ++i)
    {
        printf("1-- %s, %d\n", vec[i].getHost().toStdString().c_str(), vec[i].getPort());
        printf("2-- %s, %d\n", vec[i]["host"].toString().toStdString().c_str(), vec[i]["port"].toInt());
    }

    set(data5);

    return a.exec();
}
